package com.xnyzc.lhy.order.service.impl.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.constant.MessageConstant;
import com.xnyzc.lhy.common.entity.Qw;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.order.entity.vehicle.OaSysVehicle;
import com.xnyzc.lhy.order.entity.vehicle.OaSysVehicleMaintain;
import com.xnyzc.lhy.order.mapper.vehicle.OaSysVehicleMaintainMapper;
import com.xnyzc.lhy.order.mapper.vehicle.OaSysVehicleMapper;
import com.xnyzc.lhy.order.service.system.IOaSysVehicleMaintainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 车辆保养表 服务实现类
 * </p>
 *
 * @author lhy@xnyzc.com
 * @since 2019-07-24
 */
@Service
public class OaSysVehicleMaintainServiceImpl extends ServiceImpl<OaSysVehicleMaintainMapper, OaSysVehicleMaintain> implements IOaSysVehicleMaintainService {

    @Autowired
    private OaSysVehicleMaintainMapper maintainMapper;

    @Autowired
    private OaSysVehicleMapper vehicleMapper;

    /**
     * 根据车辆Id分页获取
     *
     * @param vehicleId 车辆Id
     * @param page      当前页
     * @param limit     每页显示数据数量
     */
    @Override
    public Rv vehicleMaintainList(Long vehicleId, Integer page, Integer limit) {
        //验证车辆ID合法性
        OaSysVehicle oaSysVehicle = vehicleMapper.selectById(vehicleId);
        if (null == oaSysVehicle) {
            return Rv.wrap(EErrorCode.argCheckErr, MessageConstant.VEHICLE_ID_ERROR);
        }
        //根据车辆主键查询车辆查询列表
        IPage<OaSysVehicleMaintain> iPage = new Page<>();
        iPage.setCurrent(page);
        iPage.setSize(limit);
        Qw qw = Qw.create();
        qw.eq("vehicle_id", oaSysVehicle.getVehicleId());
        IPage<OaSysVehicleMaintain> oaSysVehicleMaintainIPage = maintainMapper.selectPage(iPage, qw);
        //封装返回数据
        Map<String, Object> result = new HashMap<>();
        result.put("vehicle", oaSysVehicle);
        result.put("vehicleMaintain", oaSysVehicleMaintainIPage);
        return Rv.wrap(EErrorCode.success, result);
    }

    /**
     * 根据保养Id查询保养信息与车辆信息
     *
     * @param maintainId 保养ID
     */
    @Override
    public Rv getVehicleMaintain(Long maintainId) {
        //验证车辆保养ID合法性
        OaSysVehicleMaintain oaSysVehicleMaintain = maintainMapper.selectById(maintainId);
        if (null == oaSysVehicleMaintain) {
            return Rv.wrap(EErrorCode.argCheckErr, MessageConstant.VEHICLE_MAINTAIN_ID_ERROR);
        }
        //根据车辆保养信息查询车辆信息
        OaSysVehicle oaSysVehicle = vehicleMapper.selectById(oaSysVehicleMaintain.getVehicleId());
        if (null == oaSysVehicle) {
            return Rv.wrap(EErrorCode.unData, MessageConstant.VEHICLE_INFO_NULL);
        }

        //封装返回返回数据
        Map<String, Object> result = new HashMap<>();
        result.put("vehicle", oaSysVehicle);
        result.put("vehicleMaintain", oaSysVehicleMaintain);
        return Rv.wrap(EErrorCode.success, result);
    }

    /**
     * 添加保养信息
     *
     * @param vehicleMaintain 保养信息实体类
     */
    @Override
    public Rv addVehicleMaintain(OaSysVehicleMaintain vehicleMaintain) {
        //验证数据合法性
        OaSysVehicle oaSysVehicle = vehicleMapper.selectById(vehicleMaintain.getVehicleId());
        if (null == oaSysVehicle) {
            return Rv.wrap(EErrorCode.argCheckErr, MessageConstant.VEHICLE_ID_ERROR);
        }
        //添加保养信息
        boolean isTrue = maintainMapper.insert(vehicleMaintain) > 0 ? true : false;
        return Rv.wrap(isTrue ? EErrorCode.success : EErrorCode.dataParseError);
    }
}
